package com.gusha.config;

import javax.servlet.MultipartConfigElement;

import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.gusha.interceptor.CommonExceptionHandler;
import com.gusha.interceptor.LoginAdminInterceptor;

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

	// 登录拦截，操作日志拦截
	@Bean
	public HandlerInterceptor getLoginAdminInterceptor() {
		return new LoginAdminInterceptor();
	}

	// 权限拦截
//	@Bean
//	public HandlerInterceptor getAuthRoleInterceptor() {
//		return new AuthRoleInterceptor();
//	}

	// 配置全局异常信息
	@Bean
	public CommonExceptionHandler exceptionHandler() {
		return new CommonExceptionHandler();
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(getLoginAdminInterceptor()).addPathPatterns("/gx/**");
		WebMvcConfigurer.super.addInterceptors(registry);
	}
//    @Override  
//    public void addCorsMappings(CorsRegistry registry) {  
//        registry.addMapping("/gx/**")  
//                .allowedOrigins("*")  
//                .allowCredentials(true)  
//                .allowedMethods("GET", "POST", "DELETE", "PUT","PATCH")  
//                .maxAge(3600);  
//    } 
	 @Bean
	    public MultipartConfigElement multipartConfigElement() {
	        MultipartConfigFactory factory = new MultipartConfigFactory();
	        //  单个数据大小
	        factory.setMaxFileSize("100MB"); // KB,MB
	        /// 总上传数据大小
	        factory.setMaxRequestSize("100MB");
	        return factory.createMultipartConfig();
	    }


}
